import Vue from 'vue';
import Router from 'vue-router';
import Layout from '@/layout';

Vue.use(Router);

// const rules = ['admin', 'editor', 'viewer'];

export const constantRoutes = [
  {
    path: '/redirect',
    component: Layout,
    hidden: true,
    children: [
      {
        path: '/redirect/:path(.*)',
        component: () => import('@/views/redirect/index'),
      },
    ],
  },
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true,
  },
  {
    path: '/404',
    component: () => import('@/views/error-page/404'),
    hidden: true,
  },
  {
    path: '/401',
    component: () => import('@/views/error-page/401'),
    hidden: true,
  },
  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [
      {
        path: 'dashboard',
        component: () => import('@/views/dashboard/index.vue'),
        name: 'Dashboard',
        meta: { title: '数据看板', icon: 'dashboard', affix: true },
      },
    ],
  },
];

/**
 * asyncRoutes
 * 基于用户角色动态加载路由
 */
export const asyncRoutes = [
  {
    path: '/permission1',
    component: Layout,
    name: 'Permission1',
    hidden: false,
    alwaysShow: true, // will always show the root menu
    meta: {
      id: 'permission1',
      title: '采购管理',
      icon: 'lock',
      // roles: [rules[0]] // you can set roles in root nav
    },
    children: [
      {
        path: 'street',
        component: () => import('@/views/permission'),
        name: 'street',
        meta: {
          id: 'permission1/street',
          title: '采购订单',
          icon: 'lock',
          // roles: [rules[0]] // you can set roles in root nav
        },
      },
      {
        path: 'villages',
        component: () => import('@/views/permission/villages/index.vue'),
        name: 'villages',
        meta: {
          id: 'permission1/villages',
          title: '采购入库',
          icon: 'lock',
          // roles: [rules[0]] // you can set roles in root nav
        },
      },
      {
        path: 'inoculate',
        component: () => import('@/views/permission/permission_inoculate/index.vue'),
        name: 'inoculate',
        meta: {
          id: 'permission1/inoculate',
          title: '采购退货',
          icon: 'lock',
          // roles: [rules[0]] // you can set roles in root nav
        },
      },
    ],
  },
  {
    path: '/viewer',
    component: Layout,
    name: 'viewer',
    hidden: false,
    alwaysShow: true, // will always show the root menu
    meta: {
      id: 'permission2',
      title: '销售管理',
      icon: 'dashboard',
      // roles: [rules[0], rules[3]] // you can set roles in root nav
    },
    children: [
      {
        path: 'viewer',
        component: () => import('@/views/statement/viewer'),
        name: 'viewer',
        meta: {
          id: 'permission2/viewer',
          title: '报价单',
          icon: 'dashboard',
          // roles: [rules[0], rules[3]] // you can set roles in root nav
        },
      },
      {
        path: 'personnel',
        component: () => import('@/views/statement/personnel'),
        name: 'personnel',
        meta: {
          id: 'permission2/personnel',
          title: '销售合同',
          icon: 'dashboard',
          // roles: [rules[0], rules[3]] // you can set roles in root nav
        },
      }
    ],
    
  },
  {
    path: '/permission3',
    component: Layout,
    hidden: false,
    redirect: '/permission3/index',
    alwaysShow: true, // will always show the root menu
    meta: {
      id: 'permission3',
      title: '库存管理',
      icon: 'edit',
      // roles: [rules[0], rules[1]] // you can set roles in root nav
    },
    children: [
      {
        name: 'editor',
        path: 'index',
        component: () => import('@/views/community/editor'),
        meta: {
          id: 'permission3/editor',
          title: '其他入库单',
          icon: 'edit',
          // roles: [rules[0], rules[1]] // you can set roles in root nav
        },
      },
    ],
  },
  {
    path: '/common',
    component: Layout,
    hidden: false,
    redirect: '/common/index',
    alwaysShow: true, // will always show the root menu
    meta: {
      id: 'permission3',
      title: '财务管理',
      icon: 'edit',
      // roles: [rules[0], rules[1]] // you can set roles in root nav
    },
    children: [
      {
        name: 'editor',
        path: 'index',
        component: () => import('@/views/common/index'),
        meta: {
          id: 'permission3/editor',
          title: '付款单',
          icon: 'edit',
          // roles: [rules[0], rules[1]] // you can set roles in root nav
        },
      },
    ],
  },
  {
    path: '/statistics',
    component: Layout,
    hidden: false,
    redirect: '/statistics/index',
    alwaysShow: true, // will always show the root menu
    meta: {
      id: 'permission3',
      title: '基础数据',
      icon: 'edit',
      // roles: [rules[0], rules[1]] // you can set roles in root nav
    },
    children: [
      {
        name: 'index',
        path: 'index',
        component: () => import('@/views/Population_statistics/index'),
        meta: {
          id: 'permission3/editor',
          title: '新增仓库',
          icon: 'edit',
          // roles: [rules[0], rules[1]] // you can set roles in root nav
        },
      },
      {
        name: 'index2',
        path: 'index2',
        component: () => import('@/views/Population_statistics/index2'),
        meta: {
          id: 'permission3/editor',
          title: '新增客户',
          icon: 'edit',
          // roles: [rules[0], rules[1]] // you can set roles in root nav
        },
      }
    ],
  },
  // 路由模块
  { path: '*', redirect: '/404', hidden: true },
];

const createRouter = () =>
  new Router({
    // mode: 'history', // require service support
    scrollBehavior: () => ({ y: 0 }),
    routes: constantRoutes,
  });

const router = createRouter();

// for debug
window.rotuer = router;

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter();
  router.matcher = newRouter.matcher; // reset router
}

export default router;
